package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.common.annotation.DataScope;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.PageUtils;
import com.xjy.system.domain.SysTransactionUnit;
import com.xjy.system.domain.bo.SysTransactionUnitAddBo;
import com.xjy.system.domain.bo.SysTransactionUnitEditBo;
import com.xjy.system.domain.bo.SysTransactionUnitQueryBo;
import com.xjy.system.domain.vo.SysTransactionUnitVo;
import com.xjy.system.mapper.SysTransactionUnitMapper;
import com.xjy.system.service.ISysTransactionUnitService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 往来(单位-人)信息Service业务层处理
 *
 * @author zc
 * @date 2022-02-09
 */
@Service
public class SysTransactionUnitServiceImpl extends ServicePlusImpl<SysTransactionUnitMapper, SysTransactionUnit> implements ISysTransactionUnitService {

	@Override
	public SysTransactionUnitVo queryById(Long id) {
		return getVoById(id, SysTransactionUnitVo.class);
	}

	@Override
	@DataScope(deptAlias = "d",transaction = "t")
	public TableDataInfo queryPageList(SysTransactionUnit sysTransactionUnit) {
		List<SysTransactionUnit> list = baseMapper.queryPageList(sysTransactionUnit);
		return  PageUtils.getPageInfo(list);
	}

	@Override
	public List<SysTransactionUnitVo> queryList(SysTransactionUnitQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysTransactionUnitVo.class);
	}

	private LambdaQueryWrapper<SysTransactionUnit> buildQueryWrapper(SysTransactionUnitQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysTransactionUnit> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getProjId() != null, SysTransactionUnit::getProjId, bo.getProjId());
		lqw.eq(bo.getType() != null, SysTransactionUnit::getType, bo.getType());
		lqw.eq(bo.getAuditStatus() != null, SysTransactionUnit::getAuditStatus, bo.getAuditStatus());
		lqw.like(StrUtil.isNotBlank(bo.getName()), SysTransactionUnit::getName, bo.getName());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysTransactionUnitAddBo bo) {
		SysTransactionUnit add = BeanUtil.toBean(bo, SysTransactionUnit.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysTransactionUnitEditBo bo) {
		SysTransactionUnit update = BeanUtil.toBean(bo, SysTransactionUnit.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysTransactionUnit entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
